package cn.wyl.order.service.impl;

import cn.wyl.basic.util.LoginContext;
import cn.wyl.basic.util.PageList;
import cn.wyl.order.domain.OrderPetAcquisition;
import cn.wyl.order.dto.OrderDto;
import cn.wyl.order.mapper.OrderPetAcquisitionMapper;
import cn.wyl.order.query.OrderPetAcquisitionQuery;
import cn.wyl.order.service.IOrderPetAcquisitionService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.domain.Shop;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.org.mapper.ShopMapper;
import cn.wyl.pet.domain.Pet;
import cn.wyl.pet.domain.PetDetail;
import cn.wyl.pet.domain.SearchMasterMsg;
import cn.wyl.pet.mapper.PetDetailMapper;
import cn.wyl.pet.mapper.PetMapper;
import cn.wyl.pet.mapper.SearchMasterMsgMapper;
import cn.wyl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl<PetDetailmapper> extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public PageList<OrderPetAcquisition> queryList(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //获取当前登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取管理员信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果是店铺管理员
        if(employee.getShop_id()!=null){//店铺管理员：店长 or 店员
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findById(employee.getShop_id());
            //如果店铺管理员ID 与 当前登录人id不一样 就不是店长
            if(shop.getAdminId()!=employee.getId()){
                //店员只能查询自己的订单
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    //订单确认
    @Override
    public void confirm(OrderDto orderDto) {
        //orderId ,支付方式 ，成交价
        //1.生成宠物信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,orderDto.getMoney());
        petMapper.save(pet);
        PetDetail petDetail = new PetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.save(petDetail);


        //2.改订单状态为1，价格为成交价，支付方式
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //3.改寻主消息状态2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }


    //取消订单
    @Override
    public void delete(Long id) {
        //1.将寻主消息放入寻主池：msg的state=3 、shopId=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //修改订单状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);

    }
}
